﻿namespace 方法参数
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 默认参数
            Person person = TestFunc1("YEJING");

            // 命名参数
            person = TestFunc1("zhangshan", sex : "女");

            Console.WriteLine(person);


            List<People> list = new List<People>();

            addPeople2List(list); // 这种方式下，list没有元素
            foreach (People p in list)
            {
                Console.WriteLine($"{p.Name} {p.Age} {p.Gender}");
            }


            Console.WriteLine("------------------------------------------------");


            List<People> list2 = new List<People>();
            addPeople2List2(ref list2);
            foreach (People p in list2)
            {
                Console.WriteLine($"{p.Name} {p.Age} {p.Gender}");
            }
        }

        /// <summary>
        /// 带默认值的函数参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        static Person TestFunc1(string name,int age = 30,string sex = "男")
        {
            Person person = new Person()
            { 
                Name = name,
                Age = age,
                Sex = sex
            
            };

            return person;
        }

        private static void addPeople2List(List<People> list)
        {
            List<People> _list = new List<People>();

            People p1 = new People();
            p1.Name = "yejing";
            p1.Age = 10;
            p1.Gender = 'N';

            _list.Add(p1);

            list = _list;
        }

        private static void addPeople2List2(ref List<People> list)
        {
            List<People> _list = new List<People>();

            People p1 = new People();
            p1.Name = "yejing";
            p1.Age = 10;
            p1.Gender = 'N';

            _list.Add(p1);

            list = _list;
        }
    }

    internal class Person
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Sex { get; set; }

        public override string? ToString()
        {
            return $"Name:{Name},Age:{Age},Sex:{Sex}";
        }
    }

    class People
    {
        public String Name { get; set; }
        public int Age { get; set; }
        public char Gender { get; set; }
    }

}
